export default {
  namespaced: true,
  state: ()=>({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '', // 登录标识
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
  }),
  mutations: {
    updateAddress(state, address) {
      state.address = address;
      this.commit('m_user/saveToLocal');
    },
    // 保存用户地址到本地
    saveToLocal(state){
      uni.setStorageSync('address', JSON.stringify(state.address));
    },
    updateUserinfo(state, userinfo){
      state.userinfo = userinfo;
      this.commit('m_user/saveUserinfoToLocal');
    },
    // 保存用户信息到本地
    saveUserinfoToLocal(state){
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo));
    },
    // 更新token
    updateToken(state, token){
      state.token = token;
      this.commit('m_user/saveTokenToLocal');
    },
    // 存储token到本地
    saveTokenToLocal(state){
      uni.setStorageSync('token', state.token);
    }
  },
  getters: {
    // 返回用户选择的地址字符串
    addstr(state) {
        if (!state.address.provinceName) return '';
        return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo;
      }
  }
}